#include "string_utils.h"

string_main_t
explode(char *instr,char *str) {
	char *start,*p,*q,*tp;
	int leninstr,lenstr,a,i,j,found,numstr;

	string_main_t 	ret=(string_main_t)mycalloc(sizeof(struct string_main),1);
	string_cell_t	guard=(string_cell_t)mycalloc(sizeof(struct string_cell),1);
	string_cell_t	current_sc=(string_cell_t)mycalloc(sizeof(struct string_cell),1);	
	
	leninstr=strlen(instr);
	lenstr=strlen(str);
	i=0;
	j=0;
	found=0;
	numstr=0;
	
	start=p=q=instr;  
	q+=lenstr;
	tp=EMPTY;
	
	for(;i<leninstr;i++,p++,q++) {
		a=strncmp(p,str,lenstr);
		if(a==0) { //found
			if(!strncmp(start,str,lenstr)) {
				start+=lenstr;
				continue;
			}
			found=1;
		}
		if(found) {
			string_cell_t sc=(string_cell_t)mycalloc(sizeof(struct string_cell),1);
			printf("%d: sc==%p(%d)\n",numstr,sc,sizeof(struct string_cell));
			if(numstr==0) {
				ret->guard=guard;
				sc->next_sc=ret->guard;
			//	ret->guard=guard;
			//	ret->last_sc=sc;
			} else
				sc->next_sc=current_sc;
			current_sc=sc;
			//MY_MALLOC(sc->name_sc,char*,strlen(EMPTY));
			//MY_MALLOC(sc->val_sc,char*,(p-start));
			//printf("scnamesc1:%p\n",sc->name_sc);
			//MY_STRCPY(sc->name_sc,EMPTY,strlen(EMPTY));
			//printf("scnamesc1:%p\n",sc->name_sc);
			//int l=p-start;
			//printf("%d\n",l);
			//printf("%s\n",start);
			//printf("scvalsc1:%p\n",sc->val_sc);
			//printf("start:%p\n",start)
			//MY_STRCPY(sc->val_sc,EMPTY,strlen(EMPTY)); 
			//MY_STRCPY(sc->val_sc,start,l);
			//printf("scvalsc2:%p\n",sc->val_sc);
			sc->name_sc=empty_string;
			sc->val_sc=(char*)mymalloc((p-start));
#ifdef WIN32
			strncpy_s(sc->val_sc,(rsize_t)(p-start)+1,start,_TRUNCATE);
#else			
			mystrncpy(sc->val_sc,start);
#endif
			sc->len_sc=strlen(sc->val_sc);
			sc->pos_sc=i;
			printf("%d: sc->next_sc==%p(%d)\n",numstr,sc->next_sc,sizeof(struct string_cell));
			if(numstr==58)
				ret->current_sc=sc;
			
			ret->first_sc=sc;
			//ret->current_sc=sc;	// domyslnie poczatek, ew. manipulacja przy dalszych fu.
//printf("%d: global_current_sc: %p\n",numstr,global_current_sc);
printf("\n");
			start=q;
			found=0;
			numstr+=1;
			//dump_sc(sc);
		}
	}
	
	if(start<p) {
		string_cell_t sc=(string_cell_t)mycalloc(sizeof(struct string_cell),1);
		printf("%d: sc==%p(%d)\n",numstr,sc,sizeof(struct string_cell));
		if(numstr==0) {
				ret->guard=guard;
				sc->next_sc=ret->guard;
		}
		//MY_MALLOC(sc->name_sc,char*,strlen(EMPTY));
		//MY_MALLOC(sc->val_sc,char*,(p-start));
		//MY_STRCPY(sc->name_sc,EMPTY,strlen(EMPTY));
		//MY_STRCPY(sc->val_sc,EMPTY,strlen(EMPTY)); //MY_STRCPY(sc->val_sc,start,l);
		sc->name_sc=(char*)mycalloc(strlen(EMPTY),1);
		sc->val_sc=(char*)mycalloc((p-start),1);
		sc->name_sc=empty_string;
#ifdef WIN32
		strncpy_s(sc->val_sc,(rsize_t)(p-start)+1,start,_TRUNCATE);
#else
		mystrncpy(sc->val_sc,start);
#endif
		sc->len_sc=strlen(sc->val_sc);
		sc->pos_sc=i;
		sc->next_sc=current_sc;
		current_sc=sc;
		printf("%d: sc->next_sc==%p(%d)\n",numstr,sc->next_sc,sizeof(struct string_cell));
		ret->first_sc=sc;
		numstr+=1;
		//printf("%d: global_current_sc: %p\n",numstr,global_current_sc);
		printf("\n");
		//dump_sc(sc);
	}
	
	if(numstr>0) {
		//MY_STRCPY(ret->name_sm,EMPTY,strlen(EMPTY));
		//MY_STRCPY(ret->val_sm,instr,leninstr);
		ret->name_sm=empty_string;
		ret->val_sm=(char*)mycalloc(leninstr,1);
#ifdef WIN32
		strncpy_s(ret->val_sm,(rsize_t)leninstr,instr,_TRUNCATE);
#else
		mystrncpy(ret->val_sm,instr);
#endif
		//strncpy(ret->val_sm,instr,leninstr);
		ret->len_sm=leninstr;
		ret->len_cells=numstr;
		ret->next_sm=global_current_sm;
		global_current_sm=ret;
		//if(!global_strings->glen) {
		//	DEBUG
		//	global_strings->start=ret;
		//	global_strings->glen=1;
		//}
		//DEBUG
		//dump_sm(ret);
		return ret;
	} else
		return NULL;
} /*end*/
